package com.cqupt.qualityevaluation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqupt.qualityevaluation.controller.VO.evaluateindexVO;
import com.cqupt.qualityevaluation.controller.VO.evaluatetaskVO;
import com.cqupt.qualityevaluation.dao.EvaluateTaskMapper;
import com.cqupt.qualityevaluation.pojo.evaluateindex;
import com.cqupt.qualityevaluation.pojo.evaluatetask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("evaluatetask")
public class EvaluateTaskController {
    @Autowired
    private EvaluateTaskMapper evaluateTaskMapper;
    @GetMapping("/findAll")
    public List<evaluatetask> findAll(){
        return evaluateTaskMapper.selectList(null);
    }
    //分页功能
    @GetMapping("/page")
    public evaluatetaskVO queryList(Integer current, Integer size){
        evaluatetaskVO evaluatetaskVO = new evaluatetaskVO();
        IPage<evaluatetask> page=new Page<>(current,size);
        evaluateTaskMapper.selectPage(page,null);
        evaluatetaskVO.setCurrent(current);
        evaluatetaskVO.setSize(size);
        evaluatetaskVO.setTotal(page.getTotal());
        evaluatetaskVO.setEvaluatetaskList(page.getRecords());
        return evaluatetaskVO;
    }
    @PostMapping("/save")
    //通过RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的),将JSON对象转换为java对象
    public Object save(@RequestBody evaluatetask s){
        Map map=new HashMap<>();
        int result=0;
        try {
            result=evaluateTaskMapper.insert(s);
        }catch (Exception e){
            map.put("code","fail");
            map.put("msg","该评价任务号已存在");
            return map;
        }
        if (result==1){
            map.put("code","success");
            map.put("msg","添加成功");
        }
        else {
            map.put("code", "fail");
            map.put("msg","添加失败");
        }
        return map;
    }

        @DeleteMapping("/delete/{id}")
        public String delete(@PathVariable("id") String id){
//            QueryWrapper<evaluatetask> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("evaluatetaskid",id);
            evaluateTaskMapper.deleteById(id);
            return "success";
        }
    //更新信息 使用PutMapping
    @PutMapping("/update")
    public Object update(@RequestBody evaluatetask s){
        Map map=new HashMap<>();
        int result=0;
        UpdateWrapper<evaluatetask> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",s.getId());
        evaluatetask evaluatetask=new evaluatetask();
        evaluatetask.setEvaluatetaskid(s.getEvaluatetaskid());
        evaluatetask.setEvaluatetaskname(s.getEvaluatetaskname());
        evaluatetask.setEvaluateid(s.getEvaluateid());
        evaluatetask.setEvaluatetaskopentime(s.getEvaluatetaskopentime());
        evaluatetask.setEvaluatetaskendtime(s.getEvaluatetaskendtime());
        evaluatetask.setEvaluatetaskcontent(s.getEvaluatetaskcontent());
        evaluatetask.setSelectedschoolnumber(s.getSelectedschoolnumber());
        try{
            result=evaluateTaskMapper.update(evaluatetask,updateWrapper);
        }catch (Exception e){
            map.put("code","fail");
            map.put("msg","该评价任务号已存在");
            return map;
        }
        if (result==1){
            map.put("code","success");
            map.put("msg","更新成功");
        }
        else {
            map.put("code", "fail");
            map.put("msg","添加失败");
        }
        return map;
    }

    //查询单行数据（前端点击修改即回显数据到表单中）操作
    @GetMapping("/selectone/{id}")
    public evaluatetask findByevaluateindexId(@PathVariable("id") String id){
        QueryWrapper<evaluatetask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return evaluateTaskMapper.selectOne(queryWrapper);
    }

    @GetMapping("/search/1/{evaluatetaskid}")
    public List<evaluatetask> findByEvaluateTaskID(@PathVariable("evaluatetaskid") String evaluatetaskid){
        QueryWrapper<evaluatetask> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("evaluatetaskid",evaluatetaskid);
        return evaluateTaskMapper.selectList(queryWrapper);
    }
    @GetMapping("/search/2/{evaluatetaskname}")
    public List<evaluatetask> findByEvaluateTaskName(@PathVariable("evaluatetaskname") String evaluatetaskname){
        QueryWrapper<evaluatetask> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("evaluatetaskname",evaluatetaskname);
        return evaluateTaskMapper.selectList(queryWrapper);
    }

    @GetMapping("/search/3/{evaluateid}")
    public List<evaluatetask> findByEvaluateID(@PathVariable("evaluateid") String evaluateid){
        QueryWrapper<evaluatetask> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("evaluateid",evaluateid);
        return evaluateTaskMapper.selectList(queryWrapper);
    }

    @GetMapping("/search/4/{evaluatetaskopentime}")
    public List<evaluatetask> findByEvaluateTaskOpenTime(@PathVariable("evaluatetaskopentime") String evaluatetaskopentime){
        QueryWrapper<evaluatetask> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("evaluatetaskopentime",evaluatetaskopentime);
        return evaluateTaskMapper.selectList(queryWrapper);
    }

    @GetMapping("/search/5/{evaluatetaskendtime}")
    public List<evaluatetask> findByEvaluateTaskEndTime(@PathVariable("evaluatetaskendtime") String evaluatetaskendtime){
        QueryWrapper<evaluatetask> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("evaluatetaskendtime",evaluatetaskendtime);
        return evaluateTaskMapper.selectList(queryWrapper);
    }
    @GetMapping("/search/6/{evaluatetaskcontent}")
    public List<evaluatetask> findByEvaluateTaskContent(@PathVariable("evaluatetaskcontent") String evaluatetaskcontent){
        QueryWrapper<evaluatetask> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("evaluatetaskcontent",evaluatetaskcontent);
        return evaluateTaskMapper.selectList(queryWrapper);
    }


}
